package com.dolphin.service.impl;

import cn.hutool.json.JSONObject;
import com.dolphin.commons.*;
import com.dolphin.commons.utils.WxLoginCacheUtil;
import com.dolphin.forest.weChat.WeChatOffiaccountForest;
import com.dolphin.service.WeChatAdvancedService;
import net.sf.ehcache.CacheManager;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 描述:
 * ${DESCRIPTION}
 *
 * @author 林平
 * @create 2023-05-25 23:14
 * 个人博客地址：https://www.nonelonely.com
 */
@Service
public class WeChatAdvancedServiceImpl implements WeChatAdvancedService {
    private final static Logger LOGGER = LoggerFactory.getLogger(WeChatAdvancedServiceImpl.class);
    private static final CacheManager cacheManager = CacheManager.newInstance();

    @Autowired
    private WeChatOffiaccountForest weChatOffiaccountForest;

    @Override
    public String getAccessToken() throws Exception {
        LOGGER.error("---------{}");
        String accessToken = WxLoginCacheUtil.getCacheValue("accessToken");
        if (StringUtil.isNotEmpty(accessToken)) {
            return accessToken;
        }
        String appId = OptionCacheUtil.getValue(Constants.WX_OFFIACCOUNT_APP_ID);
        String appKey = OptionCacheUtil.getValue(Constants.WX_OFFIACCOUNT_APP_KEY);
        JSONObject accessTokenJSONObject = weChatOffiaccountForest.getAccessToken(appId, appKey);
        LOGGER.error("---------{}", accessTokenJSONObject.toString());
        accessToken = accessTokenJSONObject.getStr("access_token");
        if (StringUtil.isEmpty(accessToken)) {
            throw new Exception(accessToken);
        }
        WxLoginCacheUtil.putCacheValue("accessToken", accessToken, 60 * 3);
        return accessToken;
    }

    @Override
    public ResponseBean getShowqrcode() throws Exception {
        String accessToken = getAccessToken();

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String key = WxConstant.WX_QRCODE_SCENE + uuid;
        //没有扫描
        WxLoginCacheUtil.putCacheValue(key, WxConstant.WX_QRCODE_NOT_SCAN, 60 * 3);
        Map map = new HashMap();
        map.put("scene", uuid);
        String page = OptionCacheUtil.getValue(Constants.WX_OFFIACCOUNT_PAGE);
        if (StringUtil.isEmpty(page)) {
            page = "pages/login/login";
        }
        //要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"。默认是正式版。
       // map.put("env_version", "develop");
        map.put("page", page);
        byte[] release = weChatOffiaccountForest.getShowqrcode(accessToken, map);
        String s = Base64.encodeBase64String(release);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("scene", uuid);

        resultMap.put("img", WxConstant.BASE64_PNG + s);
        return ResponseBean.success(resultMap);
    }
}
